package cn.bincker.mybatis.encrypt.converter.impl;

import cn.bincker.mybatis.encrypt.exception.InvalidDataException;

import java.nio.ByteBuffer;

public class LongEncryptConverter extends BaseEncryptConverter<Long> {
    @Override
    public Long convertNonNull(byte[] data) {
        if (data.length >= Long.BYTES) {
            return ByteBuffer.wrap(data).getLong();
        }else if(data.length >= Integer.BYTES){
            return (long) ByteBuffer.wrap(data).getInt();
        }else if(data.length >= Short.BYTES){
            return (long) ByteBuffer.wrap(data).getShort();
        }else throw new InvalidDataException("long need " + Long.BYTES + " bytes but got " + data.length);
    }

    @Override
    public byte[] convertNonNull(Long object) {
        var result = new byte[Long.BYTES];
        ByteBuffer.wrap(result).putLong(object);
        return result;
    }
}
